
    <template>
  <div class="Department">
    <div class="text">库存盘点</div>
    <div class="form">
      <!-- 搜索 -->
      <el-form
        :inline="true"
        class="demo-form-inline"
        style="text-align: left"
        :label-position="labelPosition"
      >
        <div class="form-top">
          <!-- 时间 -->
          <el-form-item label="开始时间">
            <el-col :span="11" style="width: 300px; padding: 0">
              <el-date-picker
                type="date"
                placeholder="选择日期"
                v-model="form.date1"
                style="width: 100%"
                value-format="yyyy-mm-dd"
              ></el-date-picker>
            </el-col>
          </el-form-item>
          <el-form-item label="结束时间">
            <el-col :span="11" style="width: 300px; padding: 0">
              <el-date-picker
                type="date"
                placeholder="选择日期"
                v-model="form.date2"
                style="width: 100%"
                value-format="yyyy-mm-dd"
              ></el-date-picker>
            </el-col>
          </el-form-item>
          <div
            style="width: 300px; display: flex; justify-content: space-around"
          >
            <el-form-item>
              <el-button
                type="primary"
                icon="el-icon-zoom-in"
                size="mediu"
                @click="onSearch"
                >查询</el-button
              >
            </el-form-item>
            <el-form-item>
              <el-button
                type="success"
                icon="el-icon-plus"
                size="mediu"
                @click="toAdd"
                >新增</el-button
              >
            </el-form-item>
            <el-form-item>
              <el-button
                type="info"
                icon="el-icon-refresh-right"
                size="mediu"
                @click="onRest"
                >重置</el-button
              >
            </el-form-item>
          </div>
        </div>
        <div class="form-bot">
          <!-- 领用部门 -->

          <el-form-item label="审查状态">
            <el-select
              style="width: 300px"
              v-model="form.state"
              placeholder="审查状态"
              clearable
            >
              <el-option
                :key="v"
                v-for="v in select"
                :label="
                  v == 1
                    ? '待审核'
                    : v == 2
                    ? '审核通过'
                    : v == 3
                    ? '拒绝审核'
                    : '--'
                "
                :value="v"
              ></el-option>
            </el-select>
          </el-form-item>

          <!-- 搜索 -->
          <el-form-item label="搜索内容">
            <el-input
              v-model="search"
              placeholder="搜索内容"
              style="width: 300px"
            ></el-input>
          </el-form-item>
          <div style="width: 300px"></div>
        </div>
      </el-form>
    </div>
    <!-- 表单数据 -->
    <el-table
      ref="multipleTable"
      :data="tableData"
      tooltip-effect="dark"
      style="width: 100%"
      @select="check"
      id="out_table"
    >
      <el-table-column label="日期" width="120" prop="inventoryDate">
        <template slot-scope="scope">{{
          switchDate(scope.row.inventoryDate)
        }}</template>
      </el-table-column>
      <el-table-column prop="inventoryNo" label="盘点单号" align="center">
      </el-table-column>
      <el-table-column
        prop="preparedBy"
        label="制单人"
        show-overflow-tooltip
        align="center"
      >
      </el-table-column>
      <el-table-column
        prop="auditStatus"
        label="审查状态"
        show-overflow-tooltip
        align="center"
      >
        <template slot-scope="scope">{{
          scope.row.auditStatus == 1
            ? "待审核"
            : scope.row.auditStatus == 2
            ? "审核通过"
            : scope.row.auditStatus == 3
            ? "拒绝审核"
            : "---"
        }}</template>
      </el-table-column>
      <el-table-column
        prop="reviewedBy"
        label="审查人"
        show-overflow-tooltip
        align="center"
      >
      </el-table-column>
      <el-table-column fixed="right" label="操作" width="140" align="center">
        <template slot-scope="scope">
          <el-button
            class="edit_btn"
            type="primary"
            icon="el-icon-edit-outline"
            @click="openEdit(scope.row)"
            size="small"
          >
          </el-button>
          <el-button
            class="del_btn"
            type="primary"
            icon="el-icon-delete"
            @click="delInfo(scope.row)"
            size="small"
          >
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <!-- 页码 -->
    <div class="pagination">
      <div>
        <el-button
          class="down_btn"
          type="primary"
          icon="el-icon-download"
          @click="exportExcel"
          size="small"
          >导出
        </el-button>
      </div>
      <div style="display: flex; align-items: center; margin-left: 300px">
        <span style="font-size: 13px; font-weight: bold"
          >共{{ Math.ceil(total / limit) }}页/{{ total }}条数据</span
        >

        <div class="block">
          <el-pagination
            @size-change="handleSizeChange"
            @current-change="handleCurrentChange"
            :current-page.sync="page"
            :page-size="limit"
            layout="prev, pager, next, jumper"
            :total="total"
          >
          </el-pagination>
        </div>
      </div>
    </div>
  </div>
</template>
<script>
import FileSaver from "file-saver";
import * as XLSX from "xlsx";
export default {
  data: function () {
    return {
      tableData: [],
      form: {
        date1: "",
        date2: "",
        state: "",
      },
      search: "",
      page: 1,
      limit: 5,
      total: 0,
      labelPosition: "left",
      select: [1, 2, 3],
      //多选选中数组
    };
  },
  methods: {
    // 渲染
    getData: function () {
      this.$axios({
        url: "/wInventoryManagement/list",
        method: "post",
        data: {
          auditStatus: this.form.state,
          endTime: this.form.date2,
          inventoryNo: this.search,
          limit: this.limit,
          page: this.page,
          startTime: this.form.date1,
        },
      }).then(({ data }) => {
        // console.log(data);
        if (data.code == 0) {
          this.tableData = data.data;
          this.total = data.total;
        } else {
          this.$message.error("网络错误！数据加载出错！");
        }
      });
    },
    //渲染审核状态下拉框
    // toSelect: function () {
    //   this.$axios({
    //     url: "/wReceiptManagement/status",
    //     method: "post",
    //   }).then(({ data }) => {
    //     this.select.push(data.data[0].auditStatus);
    //     this.select.push(data.data[1].auditStatus);
    //     this.select.push(data.data[3].auditStatus);
    //   });
    // },
    //搜索
    onSearch: function () {
      this.getData();
    },
    //重置
    onRest: function () {
      this.form.date2 = "";
      this.search = "";
      this.form.date1 = "";
      this.form.state = "";
      this.page = 1;
      this.getData();
    },
    //页码
    toggleSelection(rows) {
      if (rows) {
        rows.forEach((row) => {
          this.$refs.multipleTable.toggleRowSelection(row);
        });
      } else {
        this.$refs.multipleTable.clearSelection();
      }
    },
    handleSelectionChange(val) {
      this.multipleSelection = val;
    },
    handleSizeChange(val) {
      console.log(`每页 ${val} 条`);
    },
    handleCurrentChange(val) {
      this.page = val;
      this.getData();
    },
    //删除
    delInfo: function (row) {
      this.$axios({
        url: "/WInventoryManagement/delete",
        method: "delete",
        params: {
          inventoryNo: row.inventoryNo,
        },
      }).then(({ data }) => {
        if (data.code == 0) {
          this.$message.success("删除成功！");
          this.getData();
        } else {
          this.$message.error(data.msg);
        }
      });
    },
    //编辑
    openEdit: function (row) {
      sessionStorage.setItem("stock", JSON.stringify(row));
      this.$router.push("/home/Kcpdedit");
      this.$store.state.xjqVuex.stock = row;
    },
    //新增
    toAdd: function () {
      this.$router.push("/home/KcpdAdd");
    },
    //时间转换
    switchDate: function (sj) {
      var now = new Date(sj);
      var year = now.getFullYear();
      var month = now.getMonth() + 1;
      var date = now.getDate();
      return year + "-" + month + "-" + date;
    },
    //多选删除
    check: function (row) {
      this.selectData.push(row[0].id);
      console.log(this.selectData);
    },
    delAll: function () {},
    //导出
    exportExcel() {
      let wb = XLSX.utils.table_to_book(document.querySelector("#out_table"));
      let wbout = XLSX.write(wb, {
        bookType: "xlsx",
        bookSST: true,
        type: "array",
      });
      try {
        FileSaver.saveAs(
          new Blob([wbout], { type: "application/octet-stream" }),
          "库存盘点.xlsx"
        );
      } catch (e) {
        if (typeof console !== "undefined") console.log(e, wbout);
      }
      return wbout;
    },
  },
  //挂在后
  mounted: function () {
    this.getData();
    // this.toSelect();
  },
};
</script>
<style lang="less" scoped>
.Department {
  padding: 30px;
}
.text {
  font-weight: 700;
  font-style: normal;
  font-size: 24px;
  color: #344563;
  margin-bottom: 20px;
}
.form {
  display: flex;
  flex-direction: column;
}
.form-top {
  display: flex;
  justify-content: space-around;
}
.form-bot {
  display: flex;
  justify-content: space-around;
}
.del_btn {
  font-size: 14px;
  color: #ffc5c5;
  background: #fef3f3;
  border-color: #fef3f3;
}

.edit_btn {
  font-size: 14px;
  color: #5fa4e2;
  background: #eef6fc;
  border-color: #eef6fc;
}
.down_btn {
  font-size: 14px;
  color: #64c8bc;
  background: #eef6fc;
  border-color: #eef6fc;
}
.pagination {
  display: flex;
  margin-top: 50px;
  justify-content: space-between;
  margin-right: 20px;
}
</style>

